package com.pro_test.proservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pro_test.commonutils.R;
import com.pro_test.proservice.entity.ProOwner;
import com.pro_test.proservice.entity.vo.OwnerQuery;
import com.pro_test.proservice.service.ProOwnerService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Captain
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/proservice/pro-owner")
@CrossOrigin
public class ProOwnerController {
    @Autowired
    private ProOwnerService proOwnerService;

    //查询业主表所有数据
    @ApiOperation(value = "查询所有业主")
    @GetMapping("findAll")
    public R findAllOwner()
    {
        List<ProOwner> list = proOwnerService.list(null);
        return R.ok().data("items",list);
    }

    //逻辑删除业主
    @ApiOperation(value = "逻辑删除业主")
    @DeleteMapping("{id}")
    public R removeOwner(@ApiParam(name = "id", value = "业主ID", required = true)
                         @PathVariable String id)
    {
        boolean flag = proOwnerService.removeById(id);
        if (flag)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    //条件查询分页
    @ApiOperation(value = "条件分页查询")
    @PostMapping("pageOwnerCondition/{current}/{limit}")
    public R pageOwnerCondition(@PathVariable long current,
                                @PathVariable long limit,
                                @RequestBody(required = false) OwnerQuery ownerQuery)
    {
        //创建page对象
        Page<ProOwner> pageOwner = new Page<>(current,limit);

        //构建条件
        QueryWrapper<ProOwner> wrapper = new QueryWrapper<>();
        String name = ownerQuery.getName();
        String sex = ownerQuery.getSex();
        String begin = ownerQuery.getBegin();
        String end = ownerQuery.getEnd();

        if (!StringUtils.isEmpty(name))
        {
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(sex))
        {
            wrapper.eq("sex",sex);
        }
        if (!StringUtils.isEmpty(begin))
        {
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end))
        {
            wrapper.le("gmt_create",end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        proOwnerService.page(pageOwner,wrapper);
        long total = pageOwner.getTotal();
        List<ProOwner> records = pageOwner.getRecords();
        return R.ok().data("total",total).data("items",records);
    }

    //添加业主
    @ApiOperation(value = "添加业主")
    @PostMapping("addOwner")
    public R addOwner(@RequestBody ProOwner proOwner)
    {
        boolean save = proOwnerService.save(proOwner);
        if (save)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    //根据业主id进行查询
    @ApiOperation(value = "查询业主")
    @GetMapping("getOwner/{id}")
    public R getOwner(@PathVariable String id)
    {
        ProOwner proOwner = proOwnerService.getById(id);
        return R.ok().data("owner",proOwner);
    }

    @GetMapping("getOwnerByMobile/{mobile}")
    public R getOwnerByMobile(@PathVariable String mobile)
    {
        QueryWrapper<ProOwner> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(mobile))
        {
            wrapper.eq("mobile",mobile);
        }
        ProOwner proOwner = proOwnerService.getOne(wrapper);
        return R.ok().data("houseId",proOwner.getHouseId()).data("ownerId",proOwner.getId());
    }

    //修改业主
    @ApiOperation(value = "修改业主")
    @PostMapping("updateOwner")
    public R updateOwner(@RequestBody ProOwner proOwner)
    {
        boolean flag = proOwnerService.updateById(proOwner);
        if (flag)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }
}

